﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using KerriganDataContract.Agency;
using System.Diagnostics;
using System.ServiceModel;

namespace KerriganService.DAC
{
    public class AgencyDAC
    {
        public static List<AgencyDC> GetListAgency()
        {
            List<AgencyDC> list = new List<AgencyDC>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = CopyListEntitiesToListBos(db.TAgencies.ToList());
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static TAgency CopyBoToEntity(AgencyDC bo)
        {
            TAgency entity = new TAgency();

            try
            {
                entity.IdAgency = bo.IdAgency;
                entity.Name = bo.Name;
                entity.IdAgencyEmployeeFK = bo.AgencyEmployee.IdAgencyEmployee;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }


        public static AgencyDC CopyEntityToBo(TAgency entity)
        {
            AgencyDC bo = new AgencyDC();

            try
            {
                bo.IdAgency = entity.IdAgency;
                bo.Name = entity.Name;
                bo.AgencyEmployee = new AgencyEmployeeDC();
                bo.AgencyEmployee = AgencyEmployeeDAC.CopyEntityToBo(entity.TAgencyEmployee);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        public static List<AgencyDC> CopyListEntitiesToListBos(List<TAgency> entities)
        {
            List<AgencyDC> bo = new List<AgencyDC>();

            try
            {
                foreach (TAgency entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
                return bo;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
    }
}